package com.iaiai.cobra.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iaiai.cobra.common.constant.YesOrNo;
import com.iaiai.cobra.common.util.StringUtil;
import com.iaiai.cobra.common.vo.ResultVo;
import com.iaiai.cobra.core.BaseController;
import com.iaiai.cobra.repository.bean.Post;
import com.iaiai.cobra.web.params.PageParams;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * Created with PostController.
 * Package: com.iaiai.cobra.system.controller
 * Author: iaiai
 * Create Time: 2024/6/24 20:49
 * QQ: 176291935
 * Email: 176291935@qq.com
 * Description:
 */
@RestController
@RequestMapping("/post")
public class PostController extends BaseController {

    @PostMapping("/list.json")
    public ResultVo page(@RequestBody PageParams params) {
        if(params==null)new PageParams();
        if(params.getPage()==null || params.getLimit()==null){
            return fail("参数异常");
        }

        Page<Post> pages = serviceManager.postService.queryPage(new Page(params.getPage(),params.getLimit()),params.getSearch());
        return success(pages);
    }

    @PostMapping("/add.do")
    public ResultVo add(@RequestBody Post params) {
        if(params==null || StringUtil.isEmpty(params.getName()) || params.getDisabled()==null){
            return fail("参数异常");
        }

        Post maxPost = serviceManager.postService.queryMax();

        params.setDel(YesOrNo.NO.getValue());
        params.setCreateTime(new Date());
        params.setModifyTime(params.getCreateTime());
        params.setSeq(maxPost==null ? 0 : (maxPost.getSeq().intValue()+1));
        serviceManager.postService.save(params);

        return success();
    }

    @PostMapping("/detail.json")
    public ResultVo detail(@RequestBody Post params) {
        if(params==null || params.getId()==null){
            return fail("参数异常");
        }
        return success(serviceManager.postService.getById(params.getId()));
    }

    @PostMapping("/edit.do")
    public ResultVo edit(@RequestBody Post params) {
        if(params==null
                || params.getId()==null
                || StringUtil.isEmpty(params.getName())
                || params.getDisabled()==null){
            return fail("参数异常");
        }

        Post post = serviceManager.postService.getById(params.getId());
        post.setModifyTime(new Date());
        post.setName(params.getName());
        post.setRemark(params.getRemark());
        post.setDisabled(params.getDisabled());
        serviceManager.postService.updateById(post);

        return success();
    }

    @PostMapping("/del.do")
    public ResultVo del(@RequestBody Post params) {
        if(params==null || params.getId()==null){
            return fail("参数异常");
        }

        Post post = serviceManager.postService.getById(params.getId());
        post.setModifyTime(new Date());
        post.setDel(YesOrNo.YES.getValue());
        serviceManager.postService.updateById(post);

        return success();
    }

}
